package com.stylefeng.guns.gateway.modular.user;

import com.alibaba.dubbo.config.annotation.Reference;
import com.stylefeng.guns.api.user.UserApi;
import com.stylefeng.guns.api.user.vo.UserInfoModel;
import com.stylefeng.guns.api.user.vo.UserModel;
import com.stylefeng.guns.gateway.common.CurrentUser;
import com.stylefeng.guns.gateway.common.vo.ResponseVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Reference(interfaceClass = UserApi.class, check = false)
    private UserApi userApi;

    @RequestMapping(value = "/register")
    public ResponseVO register(UserModel userModel) {
        if(StringUtils.isEmpty(userModel.getUserName()) || StringUtils.isBlank(userModel.getUserName())) {
            return ResponseVO.serviceFail("用户名不能为空！");
        }
        if(StringUtils.isEmpty(userModel.getPassword()) || StringUtils.isBlank(userModel.getPassword())) {
            return ResponseVO.serviceFail("密码不能为空！");
        }
        boolean isSuccess = userApi.register(userModel);
        if(isSuccess){
            return ResponseVO.success("注册成功");
        }else{
            return ResponseVO.serviceFail("注册失败");
        }
    }

    @RequestMapping(value = "/check", method= RequestMethod.POST)
    public ResponseVO check(String userName) {
        if(StringUtils.isEmpty(userName) || StringUtils.isBlank(userName)) {
            return ResponseVO.serviceFail("用户名不能为空！");
        }else{
            boolean exist = userApi.checkUserName(userName);
            if(exist){
                return ResponseVO.serviceFail("用户名已经存在");
            }else{
                return ResponseVO.success("用户名不存在");
            }
        }
    }

    @RequestMapping(value = "/logout", method= RequestMethod.GET)
    public ResponseVO logout() {
        /*
            应用：
                1.前端存储JWT（7天）：JWT的刷新
                2.服务器端会存储活动用户(30分钟)
                3.JWT里的userId为key，查找活跃用户
            退出:
                1.前端删除JWT
                2.后端服务器删除活跃用户缓存（redis）
            此处简化版：
                1.前端删除JWT
        */

        return ResponseVO.success("用户退出成功");
    }

    @RequestMapping(value = "/getUserInfo", method= RequestMethod.GET)
    public ResponseVO getUserInfo() {
        //获取当前登录用户
        String userId = CurrentUser.getUserId();
        if(StringUtils.isNotEmpty(userId) && StringUtils.isNotBlank(userId)){
            long id = Long.parseLong(userId);
            UserInfoModel userInfoModel = userApi.getUserInfo(id);
            if(userInfoModel!=null){
                return ResponseVO.success(userInfoModel);
            }else{
                return ResponseVO.serviceFail("用户信息查询失败");
            }
        }else {
            return ResponseVO.serviceFail("用户未登录");
        }
    }

    @RequestMapping(value = "/updateUserInfo", method= RequestMethod.POST)
    public ResponseVO updateUserInfo(UserInfoModel userInfoModel) {
        //获取当前登录用户
        String userId = CurrentUser.getUserId();
        if(StringUtils.isNotEmpty(userId) && StringUtils.isNotBlank(userId)){
            long id = Long.parseLong(userId);
            if(id != userInfoModel.getUserId()){
                return ResponseVO.serviceFail("只能修改您的个人信息");
            }
            UserInfoModel userInfo = userApi.updateUserInfo(userInfoModel);
            if(userInfo!=null){
                return ResponseVO.success(userInfo);
            }else{
                return ResponseVO.serviceFail("用户信息修改失败");
            }
        }else{
            return ResponseVO.serviceFail("用户未登录");
        }
    }

}
